package com.hiber;

import java.sql.Timestamp;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Worklog entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.hiber.Worklog
 * @author MyEclipse Persistence Tools
 */

public class WorklogDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(WorklogDAO.class);
	// property constants
	public static final String PERSON_ID = "personId";
	public static final String NAME = "name";
	public static final String WORK_TYPE = "workType";
	public static final String WORK_DETAIL = "workDetail";
	public static final String REQUIREMENTS = "requirements";
	public static final String DAYS_NEED = "daysNeed";
	public static final String PROGRESS_PERCENTAGE = "progressPercentage";
	public static final String HOURS_TODAY = "hoursToday";
	public static final String OVERWORK_HOURS = "overworkHours";
	public static final String NOTE = "note";

	public void save(Worklog transientInstance) {
		log.debug("saving Worklog instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Worklog persistentInstance) {
		log.debug("deleting Worklog instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Worklog findById(java.lang.Integer id) {
		log.debug("getting Worklog instance with id: " + id);
		try {
			Worklog instance = (Worklog) getSession().get("com.hiber.Worklog",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Worklog instance) {
		log.debug("finding Worklog instance by example");
		try {
			List results = getSession().createCriteria("com.hiber.Worklog")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Worklog instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Worklog as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPersonId(Object personId) {
		return findByProperty(PERSON_ID, personId);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByWorkType(Object workType) {
		return findByProperty(WORK_TYPE, workType);
	}

	public List findByWorkDetail(Object workDetail) {
		return findByProperty(WORK_DETAIL, workDetail);
	}

	public List findByRequirements(Object requirements) {
		return findByProperty(REQUIREMENTS, requirements);
	}

	public List findByDaysNeed(Object daysNeed) {
		return findByProperty(DAYS_NEED, daysNeed);
	}

	public List findByProgressPercentage(Object progressPercentage) {
		return findByProperty(PROGRESS_PERCENTAGE, progressPercentage);
	}

	public List findByHoursToday(Object hoursToday) {
		return findByProperty(HOURS_TODAY, hoursToday);
	}

	public List findByOverworkHours(Object overworkHours) {
		return findByProperty(OVERWORK_HOURS, overworkHours);
	}

	public List findByNote(Object note) {
		return findByProperty(NOTE, note);
	}

	public List findAll() {
		log.debug("finding all Worklog instances");
		try {
			String queryString = "from Worklog";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Worklog merge(Worklog detachedInstance) {
		log.debug("merging Worklog instance");
		try {
			Worklog result = (Worklog) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Worklog instance) {
		log.debug("attaching dirty Worklog instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Worklog instance) {
		log.debug("attaching clean Worklog instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}